package com.sljy.hzk.view.adapter;

import android.app.Notification;
import android.content.Context;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sljy.hzk.R;
import com.sljy.hzk.entity.UserEntity;
import com.sljy.hzk.room.AppDataBase;
import com.sljy.hzk.room.dao.UserDao;
import com.sljy.hzk.ui.LoginActivity;
import com.sljy.hzk.view.CircleImageView;

import java.util.List;

public class LoginItemMyAdapter  extends BaseAdapter{
    private List<UserEntity> Datas;
    private Context mContext;
    UserDao userDao;
    public LoginItemMyAdapter(List<UserEntity> datas, Context mContext) {
        Datas = datas;
        userDao = AppDataBase.getInstance().getUserDao();
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return Datas.size();
    }

    @Override
    public Object getItem(int position) {
        return Datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();
            //可以理解为从vlist获取view 之后把view返回给ListView
            convertView = LayoutInflater.from(mContext).inflate(R.layout.drop_item,parent,false);
            holder.name = (TextView)convertView.findViewById(R.id.item_user_name);
            holder.head = (CircleImageView) convertView.findViewById(R.id.item_user_head);
            holder.btn = (Button)convertView.findViewById(R.id.item_user_deleteBtn);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.name.setText(Datas.get(position).getAccounts().toString());
        if(Datas.get(position).getHeadPic() != null ){
            String imgPath = Datas.get(position).getHeadPic().replace("\\","/");
            Glide.with(mContext).load(imgPath).into(holder.head);
        }

           // holder.head.setImageResource(Datas.get(position).getHeadPath());
        holder.btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //删除本地历史数据
                List<UserEntity> list = userDao.getUser();
                for(UserEntity l : list){
                    if(l.getAccounts().equals(Datas.get(position).getAccounts())){
                        userDao.delete(l);
                        break;
                    }
                }
                Datas.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public final class ViewHolder {
        public TextView name;
        public CircleImageView head;
        public Button btn;
    }
}
